﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using Autofac;
using System.Reflection;
using Autofac.Integration.Mvc;
using STX.Repository;
using STX.Service;
using STX.Lib.Caching;
using STX.Lib;
using STX.Web.Framework;
using STX.Lib.Utils;

//using StackExchange.Profiling;

namespace STX.Web
{

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new[] { "STX.Web.Controllers" }
            );
        }

        /// <summary>
        /// 
        /// </summary>
        protected void Application_Start()
        {

            //MiniProfilerEF.Initialize(); 


            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            #region Dependency Injection Here
            var builder = new ContainerBuilder();
            //Register STX.Web's Controllers
            builder.RegisterControllers(Assembly.GetExecutingAssembly())
                .PropertiesAutowired();


            //Register STX.Admin's Controllers
            builder.RegisterControllers(Assembly.LoadFrom(STXConfig.CurrentExecutePath + "STX.Admin.dll"))
                .PropertiesAutowired();

            //Register STX.Web.Framework's Controllers
            builder.RegisterControllers(
                Assembly.GetAssembly(typeof (STX.Web.Framework.Security.SafeCode.SecurityController)))
                .PropertiesAutowired();

            //Register All Service
            builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(TopicService)))
                .PropertiesAutowired();

            //Register STX.Web.Frameworkd
            builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(AdminContext)))
                .PropertiesAutowired();

            //Register Cache's Provider
            builder.RegisterType<STX.Lib.Caching.Providers.MemoryCacheProvider>().As<ICacheProvider>().SingleInstance();

            //Register Safecode's Provider
            builder.RegisterType<STX.Lib.Security.SessionSafeCodeManager>().As<STX.Lib.Security.ISafeCodeProvider>();

            //Register Db Tranction
            builder.RegisterType<EfTransactionContext>().As<ITransactionContext>().InstancePerLifetimeScope();

            //Register Administrator's Context
            builder.RegisterType<AdminContext>().PropertiesAutowired().SingleInstance();

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            #region 注入所有参考代码
            //builder.RegisterType<STX.Controllers.HomeController>().PropertiesAutowired();
            //builder.RegisterControllers(typeof(STX.Controllers.HomeController).Assembly);
            //builder.RegisterControllers(Assembly.GetAssembly(typeof(HomeController))).PropertiesAutowired();
            //builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).PropertiesAutowired();
            //   t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();
            //builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();

            //builder.RegisterControllers(Assembly.GetAssembly(typeof(STX.Controllers.HomeController)));
            //builder.RegisterControllers(Assembly.GetAssembly(typeof(STX.Controllers.HomeController))).PropertiesAutowired();
            ////builder.RegisterControllers(Assembly.GetAssembly(typeof(STX.Controllers.HomeController))).PropertiesAutowired();
            //builder.RegisterAssemblyTypes(Assembly.LoadFile(@"F:\work\TOP10\STX.WebApp\bin\STX.Controllers.dll")).PropertiesAutowired();
            //builder.RegisterControllers(Assembly.LoadFile(@"F:\work\TOP10\STX.WebApp\bin\STX.Controllers.dll")).PropertiesAutowired();

            //builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //builder.RegisterControllers(Assembly.GetExecutingAssembly())
            //    .PropertiesAutowired();

            

            //builder.RegisterAssemblyTypes(typeof(STX.Service.TopicService).Assembly).PropertiesAutowired();
            

            //builder.RegisterAssemblyTypes(typeof(STX.Cache.CacheManger).Assembly).AsImplementedInterfaces();
            //builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            //builder.RegisterControllers(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();
            //builder.RegisterType<STX.Repository.UserRepositoryWeb>().As<IUserRepository>();
            //builder.RegisterType<STX.Repository.TopicRepositoryWeb>().As<ITopicRepository>();
            //builder.RegisterType<STX.Service.TopicService>().;
            
            

            

            //builder.RegisterControllers(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();
            //builder.RegisterType<STX.Cache.CacheManger>();
            //builder.
            //builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();
            #endregion

            #endregion
        }

        //protected void Application_BeginRequest()
        //{
        //    if (Request.IsLocal)
        //    {
        //        MiniProfiler.Start();
        //    }
        //}

        //protected void Application_EndRequest()
        //{
        //    MiniProfiler.Stop();
        //}
    }
}